package com.eqxiu.ieditor.msg.consumer;

import com.eqxiu.ieditor.api.HomePageSceneServiceApi;
import com.eqxiu.ieditor.api.HomePageUserServiceApi;
import com.eqxiu.ieditor.dto.HomePageUserDTO;
import com.eqxiu.ieditor.dto.HomePageUserSceneDTO;
import com.eqxiu.scene.api.SceneServiceApi;
import com.eqxiu.scene.dto.SceneDTO;
import com.eqxiu.user.api.UserApi;
import com.eqxiu.user.api.dto.User;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * @ClassName SetUpHomePageConsumer
 * @escription 设置微主页消息处理类
 * @Author suixiangyu
 * @Date 2019/4/29:46 AM
 */
@Component
public class SetUpHomePageConsumer {
    Logger logger = LoggerFactory.getLogger(SetUpHomePageConsumer.class);

    private final String HOMEPAGE_SCENE_STATUS_ON = "1";
    private final String HOMEPAGE_SCENE_STATUS_OFF = "0";

    @Autowired
    private ApplicationEventPublisher publisher;

    @Value("${rabbitmq.editor.queueName}")
    private String editorQueueName;

    @Value("${rabbitmq.editor.exchange}")
    private String editorExchange;

    @Autowired
    private HomePageUserServiceApi homePageUserServiceApi;

    @Autowired
    private HomePageSceneServiceApi homePageSceneServiceApi;

    @Autowired
    private UserApi userApi;


    @Autowired
    private SceneServiceApi sceneServiceApi;

    /**
     * @return void
     * @Description 编辑器设置微主页
     * @Author SuiXiangyu
     * @Date 2019/4/2
     * @Param [data, properties]
     **/
    @RabbitHandler
    @RabbitListener(queues = "${rabbitmq.editor.queueName}", containerFactory = "rabbitListenerContainerFactory")
    public void receiveMessage(byte[] data) {
        HashMap<String, Object> receiveMap = new HashMap<>(16);
        try {
            receiveMap = com.eqxiu.common.codec.CodecFactory.getCodec().deSerialize(data, HashMap.class);
            logger.info("QUEUE===" + editorQueueName + "===receive===" + new Gson().toJson(receiveMap));
            String user = (String) receiveMap.get("userId");
            Long sceneId = (Long) receiveMap.get("sceneId");
            String status = String.valueOf(receiveMap.get("disParkStatus"));
            /**
             *处理微主页用户信息
             **/
            HomePageUserDTO homePageUserDTO = new HomePageUserDTO();
            //获取用户基础信息
            User baseUser = userApi.getUser(user);
            //判断微主页用户是否存在
            HomePageUserDTO checkHomePageUser = homePageUserServiceApi.queryHomePageUserMessage(user);
            if (checkHomePageUser == null) {
                homePageUserDTO.setUserId(user);
                homePageUserDTO.setPhone(baseUser.getPhone());
                homePageUserDTO.setLoginId(baseUser.getLoginName());
                homePageUserDTO.setLoginTime(new Date());
                homePageUserDTO.setUpdateTime(new Date());
                homePageUserDTO.setNickName(baseUser.getNick());
                //保存微主页用户
                homePageUserServiceApi.addHomePageUserMessage(homePageUserDTO);
                logger.info("===保存微主页用户信息success===");
            }
            /**
             *处理作品信息
             **/
            //判断是开启还是移除
            if (HOMEPAGE_SCENE_STATUS_ON.equals(status)) {
                //开启
                //获取场景基础信息
                SceneDTO scene = sceneServiceApi.getScene(sceneId);
                HomePageUserSceneDTO homePageUserSceneDTO = new HomePageUserSceneDTO();
                homePageUserSceneDTO.setUserId(user);
                homePageUserSceneDTO.setSceneName(scene.getName());
                homePageUserSceneDTO.setSceneCode(scene.getCode());
                homePageUserSceneDTO.setSceneId(Integer.parseInt(String.valueOf(sceneId)));
                homePageUserSceneDTO.setCreateTime(new Date());
                homePageUserSceneDTO.setUpdateTime(new Date());
                homePageUserSceneDTO.setProperty(scene.getProperty());
                homePageUserSceneDTO.setImgStr(scene.getCover());
                homePageUserSceneDTO.setSort(1000);
                homePageUserSceneDTO.setType(2);
                homePageUserSceneDTO.setPlatform(1);
                ArrayList<HomePageUserSceneDTO> homePageUserSceneDTOS = new ArrayList<>();
                homePageUserSceneDTOS.add(homePageUserSceneDTO);
                //保存微主页作品
                homePageSceneServiceApi.addHomePageScene(homePageUserSceneDTOS);
                logger.info("===保存微主页作品信息success===");
            } else if (HOMEPAGE_SCENE_STATUS_OFF.equals(status)) {
                //关闭
                homePageSceneServiceApi.deleteHomePageScene(Integer.parseInt(String.valueOf(sceneId)));
                logger.info("===移除微主页作品信息success===");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }
}
